<template>
  <div>
    <BasicTable @register="registerTable" @fetch-success="onFetchSuccess">
      <template #toolbar>
        <a-button v-auth="['system:menu:create']" type="primary" @click="handleCreate">
          {{ t('routes.table.menuManagement.addMenu') }}
        </a-button>
        <a-button type="primary" @click="expandAll">{{ t('common.expandAll') }}</a-button>
        <a-button type="primary" @click="collapseAll">{{ t('common.collapseAll') }}</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
              auth: 'system:menu:update',
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: t('routes.table.menuManagement.confirmDelete'),
                confirm: handleDelete.bind(null, record),
              },
              auth: 'system:menu:delete',
            },
          ]"
        />
      </template>
      <template #creationTime="{ text: creationTime }">
        <span>
          {{ proxy.$filters.filterTime(creationTime) }}
        </span>
      </template>
    </BasicTable>
    <MenuDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { nextTick, getCurrentInstance, ref } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getMenuList, DeleteRoleMenu } from '/@/api/demo/menu';

  import { useDrawer } from '/@/components/Drawer';
  import MenuDrawer from './MenuDrawer.vue';

  import { columns, searchFormSchema } from './menu.data';

  import { useI18n } from '/@/hooks/web/useI18n';

  const { t } = useI18n();
  const { proxy }: any = getCurrentInstance();
  const { createMessage } = useMessage();

  const queryAll = ref('');

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, expandAll, collapseAll, getForm }] = useTable({
    // title: t('routes.table.menuManagement.menuList'),
    titleHelpMessage: 'AccountIndex',
    api: getMenuList,
    afterFetch: (data) => {
      return data;
    },
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      submitButtonOptions: {
        onClick: () => {
          let { menuName, status } = getForm().getFieldsValue();
          if (menuName != '' || status != -1) {
            queryAll.value = '1';
          } else {
            queryAll.value = '';
          }
        },
      },
      resetButtonOptions: {
        onClick: () => {
          queryAll.value = '';
        },
      },
    },
    rowKey: 'id',
    childrenColumnName: 'childList',
    beforeFetch: (params) => {
      params.permission = '';
      params.isExt = -1;
      params.show = -1;
      params.keepalive = -1;
      params.queryAll = queryAll.value;
    },
    isTreeTable: true,
    pagination: false,
    striped: false,
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    canResize: false,
    actionColumn: {
      width: 80,
      title: t('common.action'),
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: undefined,
    },
  });

  function handleCreate() {
    openDrawer(true, {
      isUpdate: false,
    });
  }

  function handleEdit(record: Recordable) {
    openDrawer(true, {
      record,
      isUpdate: true,
    });
  }

  function handleDelete(record: Recordable) {
    DeleteRoleMenu(record.id).then(() => {
      createMessage.success(t('common.delSuccessText'));
      reload();
    });
  }

  function handleSuccess() {
    reload();
  }

  function onFetchSuccess() {
    // 演示默认展开所有表项
    nextTick(expandAll);
  }
</script>
